﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 注册邮箱激活.Areas.Account.Models;

namespace 注册邮箱激活.Areas.Account.Controllers
{
    public class ManageController : Controller
    {
        EmailContext db = new EmailContext();
        //
        // GET: /Account/Manage/
        public ActionResult Register()
        {
            return View();
        }
        public ActionResult SendEmail(AccountItem accNew)
        {
            //验证
            //1. email存在与否.
            //2. 创建时间 与 激活状态

            //GUID是一种由算法生成的二进制长度为128位的数字标识符
            string validataCode = System.Guid.NewGuid().ToString();
            accNew.WhetherActive = 0;
            accNew.ValidateCode = validataCode;
            //将注册信息存入数据库, 激活状态设为1. 
            db.AccountItems.Add(accNew);
            db.SaveChanges();
            string email = accNew.Email;

            try
            {
                System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("2281880721@qq.com", "OurEDA");
                System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(email, "新人");
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.From = from;
                mail.To.Add(to);
                mail.Subject = "注册号激活";
                System.Text.StringBuilder strBody = new System.Text.StringBuilder();
                strBody.Append("点击下面的链接激活. 24小时内有效!</br />");
                strBody.Append("<a href='http://localhost:3210/Account/manage/ActivePage?customerID=" + email + "&validataCode =" + validataCode + "'>点击这里</a></br>");
                mail.Body = strBody.ToString();
                mail.IsBodyHtml = true;  //以html显示
                //设置好发送邮件服务地址
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                client.Host = "smtp.qq.com";
                //填写服务器地址相关的用户名和密码信息
                client.Credentials = new System.Net.NetworkCredential("2281880721@qq.com", "18840823312");
                //发送邮件
                client.Send(mail);
            }
            catch { }
            return View();
        }

        public ActionResult ActivePage(string email, string validataCode)
        {
            ActiveResult result = new ActiveResult();
            AccountItem acc = (from d in db.AccountItems where d.Email == email && d.ValidateCode == validataCode select d).FirstOrDefault();
            if (acc == null)
            {
                result.Statu = 2;
            }
            else if (DateTime.Now.Day - acc.CreateTime.Day > 1 || (DateTime.Now.Day - acc.CreateTime.Day == 1 && DateTime.Now.Hour > acc.CreateTime.Hour))
            {
                result.Statu = 3;
            }
            else
            {
                acc.WhetherActive = 1;
                db.Entry(acc).State = EntityState.Modified;
                db.SaveChanges();
                result.Statu = 1;
            }
            return View(result);
        }
	}
}